package vcs.media;

import java.util.ArrayList;
import java.util.HashMap;

import vcs.media.player.SongManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class DetailArtist extends ListActivity{

	public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
	String type;
	String name;
	SongManager plm;
	ArrayList<HashMap<String, String>> songsListData;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist);

		songsListData = new ArrayList<HashMap<String, String>>();

		Bundle bundle = getIntent().getExtras();
		name = bundle.getString("name");
		type = bundle.getString("type");
		
		//Get all song from artist
		if(type.equals("artist")){
			plm = new SongManager();
			this.songsList = plm.getSongFrom(name, SongManager.ARTIST);
		}
		
		//Get all song from author
		if(type.equals("author")){
			plm = new SongManager();
			this.songsList = plm.getSongFrom(name, SongManager.AUTHOR);
		}
		
		//Get all song from genre
		if(type.equals("genre")){
			plm = new SongManager();
			this.songsList = plm.getSongFrom(name, SongManager.GENRE);
		}
		
		if(!songsList.isEmpty()){
			for (int i = 0; i < songsList.size(); i++) {
				HashMap<String, String> song = songsList.get(i);
				songsListData.add(song);
			}
		}

		// Adding menuItems to ListView
		ListAdapter adapter = new SimpleAdapter(this, songsListData,
				R.layout.playlist_item, new String[] { "songTitle" },
				new int[] { R.id.songTitle });

		setListAdapter(adapter);

		// selecting single ListView item
		ListView lv = getListView();
		// listening to single listitem click
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			
				Bundle bundle = new Bundle();
				bundle.putSerializable("mediatype", songsList);
				
				Intent in = new Intent(getApplicationContext(),
						MainRun.class);
				
				in.putExtra("songIndexType", position);
				in.putExtras(bundle);
				
				if(type.equals("artist"))
					setResult(200, in);
				
				if(type.equals("author"))
					setResult(300, in);
				
				if(type.equals("genre"))
					setResult(400, in);
				
				finish();
			}
		});

	}
}
